package com.snpic.appaw.module.insure.service.s3.bo;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.snpic.appaw.module.insure.config.S3Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


@EnableAutoConfiguration
@Component
public class S3Connect {
    private final Logger log = LoggerFactory.getLogger(S3Connect.class);

    @Autowired
    private S3Config configs;

//    String access_key = "QO5KTNM3W428YFDCX9LA";
//    String secret_key = "QiCSgBmJFrRZCaFdagQIykPq1cee8OQ7YgPjMKaW";
//    AWSCredentials credentials = null;
//    String server_url = "10.80.37.31:8060";
//    AmazonS3 s3client = null;
//    ClientConfiguration config = new ClientConfiguration();


//    AmazonS3 s3client = null;
//    AWSCredentials credentials = null;
//    //设置连接时的参数
//    ClientConfiguration config = new ClientConfiguration();
    /**
     * AmazonS3 生成客户端
     * @param configs
     * @return
     */
    @Bean
    public AmazonS3 getConnect(S3Config configs) {
        AWSCredentials credentials = null;
        AmazonS3 s3client = null;
        ClientConfiguration config = new ClientConfiguration();
        System.out.println("进入getConnect ()");
        System.out.println(configs.toString());
        String access_key = configs.getAccess_key();
        String secret_key = configs.getSecret_key();
        String server_url = configs.getServer_url();
        String ossconfig = configs.getOssconfig();
        String osspath = configs.getBasePath();

        System.out.println("access_key:" + access_key);
        System.out.println("secret_key:" + secret_key);
        System.out.println("server_url:" + server_url);
        System.out.println("ossconfig:" + ossconfig);
        //设置连接方式为HTTP，可选参数为HTTP和HTTPS
        config.setProtocol(Protocol.HTTP);
//        config.setProtocol(Protocol.HTTPS);
        //设置网络访问超时时间 3秒
        config.setConnectionTimeout(5000);
        config.setUseExpectContinue(true);
        //获取访问凭证
//        AWSCredentials credentials = null;

////创建连接 通过配置文件
        if ( access_key.equals("") ||secret_key.equals("")) {
            log.info("通过服务器环境变量配置文件,获取Ak和SY  读取配置文件 ossconfig:"+ossconfig);
            log.info("osspath:"+osspath);
            s3client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new AWSStaticCredentialsProvider(new
                            ProfileCredentialsProvider(osspath , ossconfig).getCredentials()))
                    .withClientConfiguration(new ClientConfiguration()
                            .withProtocol(Protocol.HTTP)
                            .withConnectionTimeout(30000))
                    .withEndpointConfiguration(new
                            AwsClientBuilder.EndpointConfiguration(server_url, ""))
                    .withPathStyleAccessEnabled(true)
                    .build();
        }else{
            credentials = new BasicAWSCredentials(access_key, secret_key);
            log.info("通过nacos配置文件,获取Ak和SY");
            s3client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new AWSStaticCredentialsProvider(credentials))
                    .withClientConfiguration(config)
                    .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(server_url, ""))
                    .withPathStyleAccessEnabled(true)
                    .build();

        }


        System.out.println(s3client);
        return s3client;
    }
}
